# -*- coding: utf-8 -*- 
import wx
import settings
from magnifier import MagFrame
from utility import *

import wx, wx.lib.pubsub as pubsub

class Dot:
    def __init__(self):
        self.dots_list = []
        
    def append_dot(self, dot):
        self.dots_list.append(dot)
        pubsub.Publisher().sendMessage("DOTS CHANGED", 'add a dot.')
        
    def remove_dot(self,dot):
        for d in self.get_dots():
            
            if distanceByPixel(d, dot) < 10:
                self.dots_list.remove(d)
                pubsub.Publisher().sendMessage("DOTS CHANGED", 'remove one.')
         
    def clear(self):
        self.dots_list = []
        pubsub.Publisher().sendMessage("DOTS CHANGED", 'ok, it is no dots now.')
        
    def set_dots(self,dots):
        self.dots_list = dots
        pubsub.Publisher().sendMessage("DOTS CHANGED", 'new dots are setted.')
        
    def sort_dots(self, d = 'x'):
        if d=="x":self.dots_list.sort()
        else:
            self.dots_list.sort(lambda x, y: x[1]-y[1])
        
    def get_dots(self):
        return self.dots_list
    
class DotsWin(wx.Window):
    
    def __init__(self, parent, dots, imagefile='default.jpg') :
        
        wx.Window.__init__(self, parent, -1)
        self.mf_position = parent.GetPosition() # the main frame position

        self.normal_cursor = self.GetCursor()
        
        
        self.image = wx.Image(imagefile, wx.BITMAP_TYPE_ANY)
        self.model = dots
        self.redraw()
        
        self.magnifier = None
        
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRighttDown)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        
        pubsub.Publisher().subscribe(self.redraw, "DOTS CHANGED")
        pubsub.Publisher().subscribe(self.redraw_with_dotsns, "CALCULATOR GETS READY")
        
    def OnLeftDown(self,event):
        point = event.GetPositionTuple()
        self.model.append_dot(point)
        #self.redraw()
        #print dots.dots
        
    def OnRighttDown(self,event):
        point = event.GetPositionTuple()
        self.model.remove_dot(point)
        #if r:self.redraw()
        #print dots.dots
        
    def OnPaint(self,event):
        dc = wx.BufferedPaintDC(self, self.buffer)
        
    def OnMotion(self,event):
            x,y = event.GetPositionTuple()
            self.GetParent().statusbar.SetStatusText("当前位置 :%s,%s"%(str(x), str(y)) ,0)
            if settings.magnifier_on:
                self.SetCursor(wx.StockCursor(wx.CURSOR_MAGNIFIER))
                self.magnifier_show(x, y)
            else:
                self.SetCursor(self.normal_cursor)
                if self.magnifier:self.magnifier.close()
                
        
    def magnifier_show(self, x, y):
        w,h = self.image.GetWidth(),self.image.GetHeight()
        len = settings.magnifier_len
        l = len / 2.0
        x0 = x - l
        y0 = y - l 
        if x0 < 0: x0 = 0
        if y0 < 0: y0 = 0
        if x + l > w : x0 = w - len
        if y + l > h : y0 = h - len
            
        rect = wx.Rect(x0, y0, len, len)
        img = self.image.GetSubImage(rect)
        if not self.magnifier: 
            self.magnifier = MagFrame(self)
            
        self.magnifier.win.refresh(img, x-x0, y-y0)
        self.magnifier.Show(True)
            
    def set_image(self, image):
        self.image = image
        self.model.clear()
    
    def get_dc(self):    
        w = self.image.GetWidth()
        h = self.image.GetHeight()
        self.buffer = wx.EmptyBitmap(w, h)
        
        dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
        
        return dc
    
    def redraw(self, m = None):
        if m: print m
        dc = self.get_dc()
        
        self.drawImage(dc)
        self.drawDots(dc)
        #self.draw_dots_sns(dc)
        
    def redraw_with_dotsns(self, m = None):
        if m : print m
        dc = self.get_dc()
        
        self.drawImage(dc)
        self.drawDots(dc)
        self.draw_dots_sns(dc)
        
    def drawImage(self,dc):
        dc.DrawBitmap(wx.BitmapFromImage(self.image),0,0)
        
    def drawDots(self,dc):
        dc.SetPen( wx.Pen(settings.dot_color))
        dc.SetBrush(wx.Brush(settings.dot_color))
        for d in self.model.get_dots():
            x, y = d
            if settings.dot_type == "dot":
                dc.DrawCirclePoint(d, 4)
            else:
                dc.DrawLine(x, y-6,x,y+7 )
                dc.DrawLine(x-5, y,x+6,y )
                
    def draw_dots_sns(self, dc):
        f = dc.GetFont()
        f.SetWeight(wx.BOLD)
        dc.SetFont(f)
        #print f.GetWeightString()
        n = 1
        for d in self.model.get_dots():
            x, y = d
            if n < 10:
                dc.DrawText(str(n), x-12,y-8)
            else:
                dc.DrawText(str(n), x-24,y-8)
            n = n + 1
                
    def clear_dots(self):
        self.model.clear()
       